<template>
  <PageWrapper>
    <template #extra>
      <el-button type="primary" @click="handleCreate">新增</el-button>
    </template>
    <template #title>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="formInline.name" placeholder="请输入角色名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查找</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template #content>
      <el-table :data="pager.lists"  style="width: 100%" border>
        <el-table-column prop="id" label="id" width="180" />
        <el-table-column prop="name" label="名称" width="180" />
        <el-table-column prop="createdAt" label="新增时间" />
        <el-table-column fixed="right" label="Operations" width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-customer">
        <el-pagination background layout="prev, pager, next" :page-size="pager.pageSize" :total="pager.total" @change="changePage"  />
      </div>
    </template>
  </PageWrapper>

  <el-dialog v-model="dialogVisible" title="新增/编辑" width="500" draggable>
    <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        label-width="auto"
        class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="权限">
        <el-tree
            ref="treeRef"
            style="max-width: 600px"
            :data="permissionData"
            show-checkbox
            node-key="id"
            :default-checked-keys="defaultSelect"
            :props="{label:'name'}"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handelSubmit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script setup lang="js">
import PageWrapper from "@/components/pageWrapper/index.vue";
import {reactive, ref} from "vue";
import {createRole, detail, getList, updateRole} from "@/api/role.js";
import {selectPermission} from "@/api/permission.js";
import {usePaging} from "@/hooks/usePaging";
import {ElMessage} from "element-plus";

const treeRef = ref(null)
const formInline = reactive({})
const dialogVisible = ref(false)
let ruleForm = reactive({})
const defaultSelect = ref([])
const clear = () => {
  ruleForm= reactive({})
  defaultSelect.value = []
}
const permissionData = ref([])
const initPermissionData = async () => {
  permissionData.value = await selectPermission()
}
const handleEdit = async(row) => {
  clear()
  const res = await detail({id:row.id})
  defaultSelect.value = res.permissionIds
  await initPermissionData()
  Object.assign(ruleForm, {id:res.id, name: res.name})
  dialogVisible.value = true
}
const handleCreate = async () => {
  clear()
  await initPermissionData()
  dialogVisible.value = true
}
const handleClose = () => {
  clear()
  dialogVisible.value = false
}
const handleCheckedNodes = () => {
  let myPermissionIds = [];
  treeRef.value.getCheckedNodes(false,false).forEach(item => {
    if (!item.hasOwnProperty('children')) {
      myPermissionIds.push(item.id)
    }
  })
  return myPermissionIds.join(',')
}
const handelSubmit = async () => {
  ruleForm.permissionIds = handleCheckedNodes()
  const api = ruleForm.id !== '' && ruleForm.id !== undefined ? updateRole : createRole
  const res = await api({...ruleForm})
  if (res.code === '000') {
    getListFn();
    handleClose()
    ElMessage({
      message: '新增成功',
      type: 'success',
    })
  }
}
const {pager, changePage, getListFn, handleQuery} = usePaging({
  fn:getList,
  initParams:formInline
})


getListFn();

</script>

<style scoped>

</style>